<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .weui-loadmore_line .weui-loadmore__tips {
    background-color: #eee;
  }
  .rj-cell.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell label {
    color: #333;
    font-size: 14px;
    width: 44%;
    display: inline-block;
    padding: 5px 3%;
    text-align: right;
  }
  .rj-cell span {
    font-size: 14px;
    color: #333;
    display: inline-block;
    width: 50%;
  }
  .rj-cell.note label{
    float: left;
  }
  .rj-cell.note span{
    width: 80%;
    display: inline-block;
  }
  .weui-cells {
    margin-top: 0;
  }
  .weui-cells_form {
    background: #fbfbfb;
  }
  .weui-cell {
    background: #fff;
  }
  #acts span {
    font-size: 14px;
  }
  .halfbtn {
    display: inline-block;
    width: 49%;
  }
  .weui-label{
    /*width: 130px;*/
    text-align: left;
  }

  .weui-select {
    height: auto;
    line-height: normal;
  }
  *{
    list-style:none;
  }
  /*body{*/

  /*}*/
  .weui-cell_select {
    padding: 10px 15px;
  }
  .weui-cells__title {
    text-align: left;
  }
  .hide {
    display: none;
  }
  #acts {
    font-size: 0;
  }
  #acts span {
    font-size: 14px;
  }
</style>
<template>
  <div class="sendcar">
    <div style="height: 50px; line-height: 50px; background: #f7f7f7; text-align: center; font-size: 16px; border-bottom: 1px solid #bdbbbc; position: relative">
      录入库存调整
      <a href="javascript:;" @click="$router.back(-1)" style="position: absolute; left: 10px; top: 8px">
        <img src="../../assets/back.png" style="width: 20px" />
      </a>
    </div>
    <div class="weui-cells weui-cells_form">
      <form action="" method="post" id="formid">
        <div class="cellgroup">
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">所属公司</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="orgguid" v-model="orgguid">
                <option selected="" v-for="(item, index) in orgs" :key="item.guid" :value="item.guid">{{item.name}}</option>
              </select>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">库房</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="srguid" v-model="srguid">
                <option selected=""  v-for="(item, index) in storerooms" :key="item.guid" :value="item.guid">{{item.name}}</option>
              </select>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label for="" class="weui-label">库间类型</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="picktype" v-model="picktype">
                <option value="1">原材料库</option>
                <option value="2">产成品库</option>
                <option value="3">次品库</option>
              </select>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">调整类型</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="type" v-model="type">
                <option value="1">出库</option>
                <option value="2">入库</option>
              </select>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">产品</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="productid" v-model="productid">
                <option selected=""  v-for="(item, index) in products" :key="item.guid" :value="item.guid">{{item.productname}}</option>
              </select>
            </div>
          </div>
          <div class="weui-cell" v-if="picktype == 2">
            <div class="weui-cell__hd"><label class="weui-label">规格</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select spec" name="speguid" v-model="speguid">
                <option selected=""  v-for="(item, index) in specs" :key="item.guid" :value="item.guid" v-if="productid == item.productGuid">{{item.name}}</option>
              </select>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">库间</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="shguid" v-model="shguid" v-if="type==1">
                <option selected=""  v-for="(item, index) in storehouses" :key="item.storeroomguid" :value="item.storeroomguid">{{item.storeroomName}}    {{item.storeqty}}{{item.unit}}</option>
              </select>
              <select class="weui-select" name="shguid" v-model="shguid" v-if="type==2">
                <option selected=""  v-for="(item, index) in storehouses" :key="item.guid" :value="item.guid">{{item.name}}</option>
              </select>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">重量(吨)</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input weight" type="text"  name="weight" placeholder="请输入调整重量" />
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">备注</label></div>
            <div class="weui-cell__bd">
              <textarea class="weui-textarea remark" name="remark" placeholder="请输备注内容" rows="2"></textarea>
            </div>
          </div>
        </div>
        <div class="weui-btn-area">
          <a class="weui-btn weui-btn_primary halfbtn subbtn"  href="javascript:" @click="send">提交</a>
          <a class="weui-btn weui-btn_primary halfbtn"  href="javascript:" @click="$router.back(-1)" id="sub">返回</a>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'AddChangesa',
    data () {
      return {
        openid: localStorage.openid,
        orgs: [],
        storerooms: [],
        storehouses: [],
        products: [],
        specs: [],
        orgguid: '',
        srguid: '',
        shguid: '',
        productid: '',
        speguid: '',
        type: 1,
        picktype: 1
      }
    },
    created() {
      window.addEventListener('setItem', ()=> {
        this.openid = localStorage.getItem('openid');
      })
    },
    mounted () {
      //获取select备选数据
      this.getOrg();
      this.getSr();
      this.getProduct();
      this.getSpec();
    },
    watch: {
      'orgguid': function (val) {
        this.getPLInfo();
      },
      'type': function (val) {
        if (val == 1) {
          this.getPLInfo();
        } else {
          this.getSh();
        }
      },
      'srguid': function (val) {
        if (this.type == 1) {
          this.getPLInfo();
        } else {
          this.getSh();
        }
      },
      'productid': function (val) {
        if (this.type == 1) {
          this.getPLInfo();
        }
      },
      'speguid': function (val) {
        if (this.type == 1) {
          this.getPLInfo();
        }
      },
      'picktype': function (val) {
        if (this.type == 1) {
          this.getPLInfo();
        } else {
          this.getSh();
        }
      },
    },
    methods: {
      getOrg: function() {
        this.axios({
          method: 'get',
          url: '/sales/Whoutform!getOrg.action?openid=' + this.openid,
          params: {}
        }).then((response) => {
          if (response.data.orgs != undefined) {
            this.orgs = response.data.orgs;
            this.orgguid = response.data.orgs[0].guid;
          } else {
            this.toastError("系统错误");
          }

        })
      },
      getSr: function() {
        this.axios({
          method: 'get',
          url: '/sales/Salespermission!getPermisForStore.action?openid=' + this.openid,
          params: {functionTypes: 5}
        }).then(response => {
          if (response.data.msg == 'true') {
            this.storerooms = response.data.storeroom;
            this.srguid = response.data.storeroom[0].guid;
          } else if (response.data.msg == 'loginout') {
            this.toastError('请重新登录');
          } else {
            this.toastText('获取库房数据失败');
          }
        });
      },
      getPLInfo: function() {
        if (this.orgguid == '' || this.srguid == '') {
          return;
        }
        this.axios({
          method: 'get',
          url: '/sales/Whoutform!getPLInfo.action?openid=' + this.openid,
          params: {orgid: this.orgguid, strmid: this.srguid, productid: this.productid, spec: this.speguid, picktype: this.picktype}
        }).then((response) => {
          if (response.data.houses != undefined) {
            this.storehouses = response.data.houses;
            this.shguid = response.data.houses[0].guid;
          } else {
            this.toastError("系统错误");
          }

        })
      },
      getSh: function() {
        if (this.srguid == '') {
          return;
        }
        this.axios({
          method: 'get',
          url: '/sales/Whoutform!getSh.action?openid=' + this.openid,
          params: {srguid: this.srguid, shrtype: this.picktype}
        }).then((response) => {
          if (response.data.list != undefined) {
            this.storehouses = response.data.list;
            this.shguid = response.data.list[0].guid;
          } else {
            this.toastError("系统错误");
          }

        })
      },
      getProduct: function() {
        this.axios({
          method: 'get',
          url: '/sales/Whoutform!getProduct.action?openid=' + this.openid,
          params: {}
        }).then((response) => {
          if (response.data.list != undefined) {
            this.products = response.data.list;
            this.productid = response.data.list[0].guid;
          } else {
            this.toastError("系统错误");
          }

        })
      },
      getSpec: function() {
        this.axios({
          method: 'get',
          url: '/sales/Sellform!findSalesspecification.action?openid=' + this.openid,
          params: {}
        }).then((response) => {
          if (response.data.status == 'true') {
            this.specs = response.data.result;
            if (response.data.result.length > 0) {
              this.speguid = response.data.result[0].guid;
            }
          } else {
            this.toastError(response.data.result);
          }

        })
      },
      send: function() {
        if (!this.checkEmy('weight')) {
          this.toastText('重量不能为空！');
          return false;
        } else if (this.picktype == 2 && !this.checkEmy('spec')) {
          this.toastText('规格不能为空！');
          return false;
        } else if (!this.checkEmy('remark')) {
          this.toastText('备注不能为空！');
          return false;
        }
        //防止重复提交
        $('.subbtn').attr("disabled",true).css("pointer-events","none");

        this.axios({
          method: 'post',
          url: '/produce/Changestoreapply!save.action?openid=' + this.openid,
          params: this.serialize2Obj($("#formid").serializeArray())
        }).then(response => {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");

          if(response.data.msg == "success"){
            this.toastSuccess('操作成功!');
            this.$router.push({name: 'ChangesaList'});
          }else if (response.data.msg == 'outlogin'){
            this.toastError('请重新登录!');
          }else if (response.data.msg == 'error'){
            this.toastError('系统错误!');
          }
        }).catch(function (error) {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          this.toastError('系统错误');
        });

      },
    }
  }


</script>
